package com.qf.fmw.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决 post 请求的乱码问题
        request.setCharacterEncoding("utf-8");
        //解决响应乱码问题
        response.setContentType("text/html;charset=utf-8");

        //2.获取请求中的 type 值
        String type = request.getParameter("type");
        //3.获取反射对象
        Class<? extends BaseServlet> clazz = this.getClass();
        try {
            //4.通过反射对象获取方法对象
            Method method = clazz.getDeclaredMethod(type, HttpServletRequest.class, HttpServletResponse.class);
            //5.打开访问权限
            method.setAccessible(true);
            //6.调用方法
            method.invoke(this,request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
